﻿@{
    ViewBag.Title = "Form";
    Layout = "~/Views/Shared/_Form.cshtml";
}
<style type="text/css">
    #tblFile .fa-minus-circle, #tblFile .fa-plus-circle {
        margin-left: 20px;
    }
</style>
<form id="form1" method="post" action="/PatientManage/SysPatientMedicalRecord/SubmitForm" enctype="multipart/form-data">
    <div style="padding-top: 20px; margin-right: 30px;">
        <table class="form">
            <tr>
                <th class="formTitle">病历日期：</th>
                <td class="formValue">
                    <input type="hidden" name="blh" id="blh" />
                    <input type="hidden" name="keyValue" id="keyValue" />
                    <input id="rq" name="rq" type="text" class="form-control input-wdatepicker" onfocus="WdatePicker({ dateFmt: 'yyyy-MM-dd' })" />
                </td>
                <th class="formTitle">
                <td class="formValue"></td>
            </tr>
            <tr>
                <th class="formTitle" style="height: 35px;">选项：</th>
                <td class="formValue" style="padding-top: 1px;">
                    <div class="ckbox">
                        <input id="zt" name="zt" type="checkbox" checked="checked"><label for="zt">有效</label>
                    </div>
                </td>
            </tr>
            <tr>
                <th class="formTitle" valign="top" style="padding-top: 5px;">
                    备注：
                </th>
                <td class="formValue" colspan="3">
                    <textarea id="bz" name="bz" class="form-control" style="height: 60px;"></textarea>
                </td>
            </tr>
        </table>
        <table class="form" id="tblFile" style="margin:20px 10px 20px 50px;">
            <tr>
                <td class="formValue"></td>
                <td class="formValue"></td>
                <td class="formValue">
                    <i class="fa fa-plus-circle fa-lg" style="cursor:pointer;"></i>
                </td>
            </tr>
        </table>
    </div>
</form>

<script>
    var blh = $.request("blh");
    var keyValue = $.request("keyValue");
    $('#blh').val(blh);
    $('#keyValue').val(keyValue);

    $(function () {
        initControl();
        if (!!keyValue) {
            $.najax({
                url: "/PatientManage/SysPatientMedicalRecord/GetFormJson",
                data: { keyValue: keyValue },
                dataType: "json",
                success: function (data) {
                    if (data.main) {
                        $("#form1").formSerialize(data.main);
                        if (data.main.rq) {
                            $('#rq').val($.getDate({ date: data.main.rq }));
                        }
                    }
                    if (data.details) {
                        for (var i = 0; i < data.details.length; i++) {
                            insertFile(i, data.details[i].Id, data.details[i].attachName);
                        }
                    }
                }
            });
        }
        else {
            $('#zt').click(function () {
                return false;
            });
            $('#tblFile .fa-plus-circle').trigger('click');
        }
    });

    function initControl() {
        $('#tblFile').on('click', '.fa-minus-circle', function () {
            $(this).closest('tr').remove();
        });
        $('#tblFile .fa-plus-circle').click(function () {
            var arr = new Array();
            $.each($('#tblFile tr'), function () {
                var fileId = $(this).find('[type=file]').attr('id');
                if (fileId) {
                    var index = fileId.indexOfIgnoreCase('_');
                    var n = fileId.substring(index + 1);
                    arr.push(n);
                }
            });
            var iIndex = -1;
            for (var i = 0; i < 10; i++) {
                var marr = $.jsonWhere(arr, function (v) {
                    return v === i.toString();
                });
                if (marr.length == 0) {
                    iIndex = i;
                    break;
                }
            }
            if (iIndex == -1) {
                $.modalAlert("已达附件上线", 'warning');
                return;
            }
            insertFile(iIndex);
        });

    }

    function insertFile(iIndex, keyIdVal, fileNameVal) {
        var shtml = '<tr> <td class="formValue"> <input value="' + (!!keyIdVal ? keyIdVal : '') + '" type="hidden" name="dbKeyId_' + iIndex + '" id="dbKeyId_' + iIndex + '" />';
        shtml += '<input type="file" id="file_' + iIndex + '" name="file_' + iIndex + '" /></td><td class="formValue">';
        shtml += '<input value="' + (!!fileNameVal ? fileNameVal : '') + '" class="form-control" type="text" id="fileName_' + iIndex + '" name="fileName_' + iIndex + '" placeholder="附件名称" /> </td> <td class="formValue"> <i class="fa fa-minus-circle fa-lg" style="cursor:pointer;"></i> </td> </tr>';
        $(shtml).insertBefore($('#tblFile .fa-plus-circle').closest('tr'));
    }

    function AcceptClick(callBack) {
        if (!$('#form1').formValid()) {
            return false;
        }
        $("#form1").submit();
        //$.submitForm({
        //    url: "?keyValue=" + keyValue,
        //    param: $("#form1").formSerialize(),
        //    success: function () {
        //        callBack();
        //        $.modalClose();
        //    }
        //})
    }
</script>